package top.chmin.project.cloud.common.entity.column.auto;

// field import auto
// #bind.field.tableCode#

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.entity.Page;
import top.chmin.project.cloud.common.entity.auto.Entity;
import top.chmin.project.cloud.common.entity.auto.EntityDao;
import top.chmin.project.cloud.common.entity.auto.EntityFPC;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;



/**
 * chmin
 * 2023-12-27 15:10:15
 * cloud-builder
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EntityColumnService {

    @Resource
    EntityColumnPlugin plugin;

    @Resource
    EntityColumnDao entityColumnDao;

// field labelDao auto
// #bind.field.tableCode#
@Resource
EntityDao entityDao;
// $bind.field.tableCode$


    public int save(EntityColumnSave.Param param) {
        plugin.checkSave(param);
        EntityColumn entityColumn = BeanUtil.toBean(param, EntityColumn.class);
        plugin.beforeSave(entityColumn);
        int result = entityColumnDao.save(entityColumn);
        plugin.afterSave(param, entityColumn, result);
        return result;
    }

    public int updateById(EntityColumnUpdateById.Param param) {
        plugin.checkUpdateById(param);
        EntityColumn entityColumn = BeanUtil.toBean(param, EntityColumn.class);
        plugin.beforeUpdateById(entityColumn);
        int result = entityColumnDao.updateById(entityColumn);
        plugin.afterUpdateById(param, entityColumn, result);
        return result;
    }

    public int deleteById(EntityColumnDeleteById.Param param) {
        plugin.checkDeleteById(param);
        int result = entityColumnDao.deleteById(param.getId());
        plugin.afterDeleteById(param, result);
        return result;
    }

    public EntityColumnSelectById.Result selectById(EntityColumnSelectById.Param param) {
        plugin.checkSelectById(param);
        EntityColumnFPC fpc = new EntityColumnFPC().id().eq(param.getId());
        plugin.beforeSelectById(param, fpc);
        EntityColumnSelectById.Result result = entityColumnDao.getFirst(fpc.toCondition(), EntityColumnSelectById.Result.class);
        plugin.afterSelectById(param, result);
        return result;
    }

    public Page<EntityColumnSelectPage.Result> selectPage(EntityColumnSelectPage.Param param) {
        plugin.checkSelectPage(param);
        EntityColumnFPC fpc = new EntityColumnFPC();
        fpc = param.isDesc() ? fpc.id().desc() : fpc.id().asc();
        plugin.beforeSelectPage(param, fpc);
        Page<EntityColumnSelectPage.Result> result = entityColumnDao.getPageByCondition(fpc.toCondition(), EntityColumnSelectPage.Result.class);
// field label auto
// #bind.field.tableCode#
        if (CollectionUtil.isNotEmpty(result.getList())) {
            List<String> tableCodeList = result.getList().stream()
                    .map(EntityColumnSelectPage.Result::getTableCode)
                    .toList();
            Stream<Entity> tableCodeStream = entityDao.getStreamByCondition(
                    new EntityFPC()
                            .forDistinct()
                            .tableCode().in(tableCodeList)
                            .toCondition()
            );
            Map<String, Entity> tableCodeMap = tableCodeStream.collect(Collectors.toMap(
                    Entity::getTableCode,
                    t -> t
            ));
            result.getList().forEach(r -> {
                Entity entity = tableCodeMap.get(r.getTableCode());
                r.setTableCodeLabel(entity == null ? null : entity.getTableName());
            });
        }
// $bind.field.tableCode$

        plugin.afterSelectPage(param, result);
        return result;
    }

// entity select auto
// entity select auto
public List<EntityColumnFieldCodeSelect.Result> selectFieldCode(EntityColumnFieldCodeSelect.Param param) {
    plugin.checkSelectFieldCode(param);
    EntityColumnFPC fpc = new EntityColumnFPC();
    plugin.beforeSelectFieldCode(param, fpc);
    Stream<EntityColumnFieldCodeSelect.Result> result = entityColumnDao.getStreamByCondition(fpc.toCondition(), EntityColumnFieldCodeSelect.Result.class)
            .peek(r -> {
                r.setValue(r.getFieldCode());
                r.setLabel(r.getFieldName());
            });
    List<EntityColumnFieldCodeSelect.Result> list = result.toList();
    plugin.afterSelectFieldCode(param, list);
    return list;
}



// entity search auto

}
